//
//Copyright 2009 Google Inc.
//
//Licensed under the Apache License, Version 2.0 (the "License");
//you may not use this file except in compliance with the License.
//You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
//Unless required by applicable law or agreed to in writing, software
//distributed under the License is distributed on an "AS IS" BASIS,
//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//See the License for the specific language governing permissions and
//limitations under the License.
//

/**
 * @fileoverview This script contains a function that works as a panel resizer
 * on the landing page. There is a slider arrow on the landing page, clicking
 * on which, left navigation bar is hidden. This is necessary for better PDF
 * view in case of landing page containg PDF.
 */

/**
 * Toggles the visibility of left navigation bar and resizes the other UI
 * widgets like youtube/pdf viewer. When the PDF viewer is
 * resized, load_viewport function needs to be called to restore the PDF viewer
 * state.
 * @param {digitalContentWizard.Viewport} viewportWidget Viewport object.
 */
digitalContentWizard.panelResizer = function(viewportWidget){
  var pageSlider = window.$('#dcw-slider');
  var navigationBar = window.$('#navcontainer');
  var viewportDiv = window.$('#viewport');
  var youtubeDiv = window.$('#entityNode');
  var writelyDiv = window.$('#writely');
  var trixDiv = window.$('#trix');
  var sliderArrow = window.$('#dcw-panel-arrow');

  // When clicked, toggle the visibility of the <div#navcontainer> div.
  sliderArrow.click(function() {
    navigationBar.toggle();

    // Check the current state of navigation bar.
    if (navigationBar.is(':visible')) {
      // Reset the state of widgets present on pagecontent div to normal.
      sliderArrow.removeClass('collapsed');
      window.$('#divBody0').removeClass('collapsed');
      window.$('#divBody1').removeClass('collapsed');
      window.$('#dcw-pagecontent').removeClass('collapsed');
      if (trixDiv.length) {
        trixDiv.removeClass('collapsed')
      }
      if (writelyDiv.length) {
        writelyDiv.removeClass('collapsed')
      }
      if (youtubeDiv.length) {
        youtubeDiv.removeClass('collapsed')
      }
      if (viewportDiv.length) {
        viewportDiv.removeClass('collapsed')
        if (viewportWidget) {
          viewportWidget.load();
        }
      }
    }
    else {
      // Change the state of widgets present on pagecontent div to collapsed.
      sliderArrow.addClass('collapsed');
      window.$('#divBody0').addClass('collapsed');
      window.$('#divBody1').addClass('collapsed');
      window.$('#dcw-pagecontent').addClass('collapsed');
      if (window.jQuery.browser.msie && window.jQuery.browser.version < 7)
        pageSlider.css('left', '');
      if (trixDiv.length) {
        trixDiv.addClass('collapsed')
      }
      if (writelyDiv.length) {
        writelyDiv.addClass('collapsed')
      }
      if (youtubeDiv.length) {
        youtubeDiv.addClass('collapsed')
      }
      if (viewportDiv.length) {
        viewportDiv.addClass('collapsed')
        if (viewportWidget) {
          viewportWidget.load();
        }
      }
    }
  });
};
